﻿jQuery(function ($) {

    var refreshgrid;
    var shuDi = "北京";
    var doAction = function ($container) {
        var mid = $container.attr("mid");
        var tid = $container.attr("tid");
        var uid = $container.attr("uid");
        var proxy = new $g.sm(mid, tid);
        var region = $container.attr("region");
        var $tab = $container.find(".i-list-tab");
        $tab.tabs({
            select: function (event, ui) {
                $container.find("input[name=s_ShuDi]").val($(ui.tab).attr("shudi"));
                refreshgrid();
                seachResult();
                searchHJDanWei();
                searchChuFa();
            }
        });
        var $tbody = $container.find(".i-list-action-list>tbody").template({
            autoBind: false
        })
        //list     

       
       

        //新增本地交易信息采集
        var $actionArea2 = $container.find("div.i-bendi-gongcheng-caiji");

        $actionArea2.wrapInner("<form id='bendicaijiForm' />");
        var $form2 = $actionArea2.find("#bendicaijiForm");
        var validator2 = $form2.validate();
        $actionArea2.delegate(".i-ico-save-jiaoyi", "click", function () {
            var valided2 = $form2.valid();
            if (valided2) {
                var d = $g.getForm($actionArea2);
                proxy.action("Update", d, function (data) {
                    if (data) {
                        alert("保存成功！");
                    }
                    else {
                        alert("保存失败！");
                    }
                });
            }
            return false;
        });
        var $containerTable = $("table.i-bendi-gongcheng-caiji-table", $container);
        var $tongjiTable = $("table.i-company-tongjibiao", $container);
        var $caijiTable = $("table.i-gongchengcaiji-caiji-table", $container);
        var $gedishigongtongji = $("table.i-company-tongjibiao-gedishigong", $container);

        $caijiTable.delegate("#QuYus", "change", function () {
            $caijiTable.find("input[name=QuYu]").val($caijiTable.find("select[name=QuYus]").val());
        });

        $containerTable.delegate("span", "click", function () {
            var aValue = $(this).attr("av");
            if (aValue == 1) {
                $containerTable.find("input[name=QuYu]").val("北京");
                $tongjiTable.css("display", "none");
                $containerTable.find(".quyuselect").css("display", "none");
                $caijiTable.css("display", "inline");
                $gedishigongtongji.css("display", "none");
            }
            else
                if (aValue == 2) {
                    $containerTable.find(".quyuselect").css("display", "inline");
                    $tongjiTable.css("display", "none");
                    $caijiTable.css("display", "inline");
                    $gedishigongtongji.css("display", "none");
                }
                else
                    if (aValue == 3) {
                        $tongjiTable.css("display", "inline");
                        $containerTable.find("table.i-gongchengcaiji-caiji-table").css("display", "none");
                        $caijiTable.css("display", "none");
                        $gedishigongtongji.css("display", "none");

                    }
                    else
                        if (aValue == 4) {
                            $gedishigongtongji.css("display", "inline");
                            $tongjiTable.css("display", "none");
                            $caijiTable.css("display", "none");
                        }
                        else
                            if (aValue == 5) {
                                $gedishigongtongji.css("display", "none");
                                $caijiTable.css("display", "none");
                            }
                            else
                                if (aValue == 6) {
                                    $gedishigongtongji.css("display", "none");
                                    $tongjiTable.hide();
                                }
        });
        refreshgrid = function () {
            if ($container.hasClass("i-company-list-action") || $container.hasClass("i-shudi-company-table") || $container.hasClass("i-chufa-list-action") || $container.hasClass("i-huojiang-list-action")) {
                var $grid = $("table.i-grid-action-list:first", $container);
                
                $grid.grid({
                    jScrollPane: false,
                    fixHeader: false,
                    jsonData: function (dataReady, index, size){
                        index = index || 1;
                        size = size || 20;
                        proxy.action("Search", {
                            pindex: index,
                            pagesize: size,
                            condition: $g.getForm($(".i-list-action-action", $container))
                        }, function (data) {
                            dataReady(data);                           
                        });
                    }
                });

            }
        }

        var $actionArea = $container.find("div.i-list-action-edit");
        $actionArea.dialog({
            modal: true,
            autoOpen: false,
            width: 'auto'
        });
        $actionArea.wrapInner("<form id='aform' />");
        var $form = $actionArea.find("#aform");
        var validator = $form.validate();

        //get
        $tbody.delegate(".i-action-edit", "click", function () {
            proxy.action("Get", {
                EditID: $(this).attr("editid")
            }, function (data) {
                $g.setForm($actionArea, data);
                $actionArea.dialog("option", "title", "编辑").dialog("open");
            });
        });
        //delete
        $tbody.delegate(".i-action-delete", "click", function () {
            var deleteid = $(this).attr("deleteid");

            confirm("确定删除该记录吗？", function () {
                proxy.action("Delete", {
                    DeleteID: deleteid
                }, function (data) {
                    refresh();
                });
            });
        });

        //验证公司名称和企业代码是否存在
        $actionArea.find(".i-company-check").change(function () {
            proxy.action("CheckExists", $g.getForm($actionArea), function (data) {
                if (data == -1) {
                    alert("企业代码已存在！");
                    $actionArea.find("button[name='btnSave']").attr("disabled", true);
                }
                else
                    if (data == 1) {
                        alert("企业名称已存在！");
                        $actionArea.find("button[name='btnSave']").attr("disabled", true);
                    }
                    else {
                        $actionArea.find("button[name='btnSave']").attr("disabled", false);
                    }
            })

        })

        ///验证处罚信息和处罚文号是否存在
        $actionArea.find(".i-chufa-check").change(function () {
            proxy.action("CheckExists", $g.getForm($actionArea), function (data) {
                if (data == -1) {
                    $actionArea.find("button[name='btnSave']").attr("disabled", true);
                    alert("文号已经存在！");
                }
                else
                    if (data == 1) {

                        alert("该处罚已经存在!");
                        $actionArea.find("button[name='btnSave']").attr("disabled", true);
                    }
                    else {
                        $actionArea.find("button[name='btnSave']").attr("disabled", false);
                    }
            });
        })
        //验证企业类型名称是否存在

        $actionArea.find(".i-companyType-check").change(function () {
            proxy.action("CheckExists", $g.getForm($actionArea), function (data) {
                if (data == 1) {
                    $actionArea.find("button[name='btnSave']").attr("disabled", true);
                    alert("该企业类型名称已存在！");
                }
                else {
                    $actionArea.find("button[name='btnSave']").attr("disabled", false);
                }
            });
        })
        //验证获奖信息
        $actionArea.find(".i-huojiang-check").change(function () {
            proxy.action("CheckExists", $g.getForm($actionArea), function (data) {
                if (data == 1) {
                    $actionArea.find("button[name='btnSave']").attr("disabled", true);
                    alert("该文号已经存在！");
                }
                else
                    if (data == -1) {
                        $actionArea.find("button[name='btnSave']").attr("disabled", true);
                        alert("该获奖信息已经存在！");
                    }
                    else {
                        $actionArea.find("button[name='btnSave']").attr("disabled", false);
                    }
            });
        })
        //验证机构名称是否存在
        $actionArea.find(".i-jigou-check").change(function () {
            proxy.action("CheckExists", $g.getForm($actionArea), function (data) {
                if (data == 1) {
                    $actionArea.find("button[name='btnSave']").attr("disabled", true);
                    alert("该机构名称已经存在！");
                }
                else {
                    $actionArea.find("button[name='btnSave']").attr("disabled", false);
                }
            });
        })

        //验证属地名称是否存在
        $actionArea.find(".i-shudi-check").change(function () {
            proxy.action("CheckExists", $g.getForm($actionArea), function (data) {
                if (data == 1) {
                    $actionArea.find("button[name='btnSave']").attr("disabled", true);
                    alert("该属地名称已经存在！");
                }
                else {
                    $actionArea.find("button[name='btnSave']").attr("disabled", false);
                }
            });
        })
        //验证主项资质名称是否存在
        $actionArea.find(".i-zhuzizhi-check").change(function () {
            proxy.action("CheckExists", $g.getForm($actionArea), function (data) {
                if (data == 1) {
                    $actionArea.find("button[name='btnSave']").attr("disabled", true);
                    alert("该主项资质名称已经存在！");
                }
                else {
                    $actionArea.find("button[name='btnSave']").attr("disabled", false);
                }
            });
        })
        //验证资质等级名称是否存在
        $actionArea.find(".i-zizhidengji-check").change(function () {
            proxy.action("CheckExists", $g.getForm($actionArea), function (data) {
                if (data == 1) {
                    $actionArea.find("button[name='btnSave']").attr("disabled", true);
                    alert("该主项等级已经存在！");
                }
                else {
                    $actionArea.find("button[name='btnSave']").attr("disabled", false);
                }
            });
        })
        //update
        $actionArea.find(".i-action-save").click(function () {
            var valided = $form.valid();
            if (valided) {
                var d = $g.getForm($actionArea);
                proxy.action("Update", d, function (data) {
                    $actionArea.dialog('close');
                    if (!$container.hasClass("i-company-list-action") && !$container.hasClass("i-chufa-list-action") && !$container.hasClass("i-huojiang-list-action")) {
                        refresh();
                    }
                    else {
                        refreshgrid();
                    }
                });
            }
            return false;
        });

        //add
        $container.find(".i-action-add").click(function () {
            $form.get(0).reset();
            validator.resetForm();
            $form.find("input").each(function () {
                var $this = $(this);
                if ($this.attr("type") == "checkbox") {
                    $this.attr("checked", $this.attr("defvalue"));
                }
                else {
                    $this.val($this.attr("defvalue"));
                }
            });
            if ($container.hasClass("i-company-list-action")) {
                initSelectItem();
            }
            if ($container.hasClass("i-chufa-list-action")) {
                initCompanyChuFa();
            }
            if ($container.hasClass("i-huojiang-list-action")) {
                initCompanyInfoItem();
            }
            $actionArea.dialog("option", "title", "新增").dialog("open");
        });
        $container.find(".i-action-search").click(function () {
            refreshgrid();
        });
        //sort
        $container.find(".i-action-saveorder").click(function () {
            var order = $.map($tbody.children(), function (tr) {
                return $(tr).attr("orderid");
            });
            proxy.action("SaveOrder", {
                Orders: order
            }, function (data) {
                refresh();
            });
        });

        //EXPORT

        $container.find(".i-action-excel").click(function () {
            location.href = location.pathname + "?tabid=" + tid + "&mid=" + mid + "&export=1&condition=";
        });
        //ImportExcel
        $container.find(".i-action-importExcel").click(function () {
            location.href = location.pathname + "?tabid=" + tid + "&mid=" + mid + "&export=2&condition=";
        });
        refresh();
        refreshgrid();
    };

    $(".i-shudi-list-action").each(function () {
        doAction($(this));
    });
});
